﻿CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

# -----------------------------------------------------------------------------
# MFC设置：第一个参数运行库T/D；第二个参数是否使用MFC
# 设置MT/使用MFC：    PC2_RUNLIB("T", TRUE)
# 设置MD/不用MFC：    PC2_RUNLIB("D", FALSE)
# -----------------------------------------------------------------------------
MACRO(PC2_MFC_SET   _STR_RUNLIB   _B_MFC)
    MESSAGE(STATUS "_STR_RUNLIB=${_STR_RUNLIB} _B_MFC=${_B_MFC} ")
    IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
        SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /M${_STR_RUNLIB}d")
        SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")
        SET(CMAKE_C_FLAGS_MINSIZEREL  "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")
        SET(CMAKE_C_FLAGS_RELWITHDEBINFO  "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")
        SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /M${_STR_RUNLIB}d")
        SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")
        SET(CMAKE_CXX_FLAGS_MINSIZEREL  "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")
        SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO  "${CMAKE_CXX_FLAGS_RELEASE} /M${_STR_RUNLIB}")

        #加入MFC支持，静态链接1 动态链接2
        if( ${_B_MFC})
            if( ${_STR_RUNLIB} MATCHES "T")
	            SET(CMAKE_MFC_FLAG 1)
            else()
                SET(CMAKE_MFC_FLAG 2)
            endif()
        endif()
    ENDIF()
ENDMACRO()

# -----------------------------------------------------------------------------
# 通用部分(无需修改)-start
# -----------------------------------------------------------------------------
# 识别cpu和操作系统，整理成变量 PC2_OS 。
# PC2_OS: 
#    "windows_x86"            32位windows程序  (x86架构)
#    "windows_x64"            64位windows程序  (x86架构)    
#    "linux_x86"              32位linux程序    (x86架构)    
#    "linux_x64"              64位linux程序    (x86架构)    
#    "linux_aarch64"          64位linux程序    (aarch64架构)
#    "macos"                  苹果程序   
#    "android_armeabi-v7a"    32位android程序                
# -----------------------------------------------------------------------------
if(CMAKE_ANDROID_NDK)
    #TODO：这里判断不够严谨
    SET(PC2_OS "android_armeabi-v7a") 
    #定义asm目录
    if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
        INCLUDE_DIRECTORIES(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi)
    elseif(${ANDROID_ABI} STREQUAL "arm64-v8a")
        INCLUDE_DIRECTORIES(${ANDROID_SYSROOT}/usr/include/aarch64-linux-android)
    else()
        INCLUDE_DIRECTORIES(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi)
    endif()
else()
    if(MSVC_CXX_ARCHITECTURE_ID)
        STRING(TOLOWER ${MSVC_CXX_ARCHITECTURE_ID} LOWERCASE_CPU)
    else()
        STRING(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} LOWERCASE_CPU)
    endif()
    #os
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
        #禁止警告
        add_compile_options(/wd4819)
        add_compile_options(/wd4996)
        if(LOWERCASE_CPU MATCHES "(x64|x86_64|amd64)")
            SET(PC2_OS "windows_x64")
        elseif (LOWERCASE_CPU MATCHES "(i[3-6]86|x86)")
            SET(PC2_OS "windows_x86")
        else ()
            message(FATAL_ERROR "Windows: Unknown CPU '${LOWERCASE_CPU}'")
        endif ()
    elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
        if(LOWERCASE_CPU MATCHES "(x64|x86_64|amd64)")
            SET(PC2_OS "linux_x64")
        elseif (LOWERCASE_CPU MATCHES "aarch64")
            SET(PC2_OS "linux_aarch64")
        elseif (LOWERCASE_CPU MATCHES "(i[3-6]86|x86)")
            SET(PC2_OS "linux_x86")
        else ()
            message(FATAL_ERROR "Linux: Unknown CPU '${LOWERCASE_CPU}'")
        endif ()
    elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
        SET(PC2_OS "macos")
    else()
        message(FATAL_ERROR "Unknown OS '${CMAKE_SYSTEM_NAME}'")
    endif()
endif()
MESSAGE(STATUS "[PC2_OS] is '${PC2_OS}'")
MESSAGE(STATUS "[CMAKE_CURRENT_LIST_DIR] =  '${CMAKE_CURRENT_LIST_DIR}'")
#C++标准
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
# -----------------------------------------------------------------------------
# 通用部分-end
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# 定义包含目录
# -----------------------------------------------------------------------------
INCLUDE_DIRECTORIES(
	${CMAKE_CURRENT_LIST_DIR}/
	#${CMAKE_CURRENT_LIST_DIR}/../
)

# -----------------------------------------------------------------------------
# 定义源目录
# -----------------------------------------------------------------------------
FILE(GLOB PC2_SRC  
	"${CMAKE_CURRENT_LIST_DIR}/pc2.cmake"
	"${CMAKE_CURRENT_LIST_DIR}/*.cpp"				
	"${CMAKE_CURRENT_LIST_DIR}/*.h" 	 
	"${CMAKE_CURRENT_LIST_DIR}/m_oswrapper/*.cpp"	
	"${CMAKE_CURRENT_LIST_DIR}/m_oswrapper/*.h" )

# -----------------------------------------------------------------------------
# 在VS中组织源码结构
# -----------------------------------------------------------------------------
MESSAGE(STATUS "[MSVC] is ${MSVC}")
IF(MSVC)
    SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
    SOURCE_GROUP(TREE  ${CMAKE_CURRENT_LIST_DIR} PREFIX  "PC2" FILES ${PC2_SRC})
endif()
